let BASE_URL = "https://szgb.tongchengbang.cc/";
// #ifdef H5
// BASE_URL = "/api";
// #endif
const request = (obj, data, loading) => {
	if (loading) uni.showLoading({
		title: "加载中",
		mask: true
	});
	let token = uni.getStorageSync("token") || ""
	let httpDefaultOpts = {
		url: BASE_URL + obj.url,
		data: data,
		beforeSend: function(xmlHttp) {
			xmlHttp.setRequestHeader("If-Modified-Since", "0");
			xmlHttp.setRequestHeader("Cache-Control", "no-cache");
		},
		method: obj.method || 'post',
		dataType: "json",
		header: {
			"Content-type": "application/json",
			"token": token
		},
	};
	// if (token) {
	//     if (httpDefaultOpts.header) {
	//         httpDefaultOpts.header["Authorization"] = "Bearer " + token;
	//     } else {
	//         httpDefaultOpts.header = { Authorization: "Bearer " + token };
	//     }
	// } else {
	//     console.log("获取不到userToken");
	// }
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts)
			.then((res) => {
				// console.log("res,res",res)
				if (loading) uni.hideLoading();
				if (res.statusCode == 200) {
					resolve(res.data);
				} else if (res.statusCode == 401) {
					uni.removeStorageSync("userInfo");
					uni.removeStorageSync("tokenInfo");
					return uni.reLaunch({
						url: "/pages/index/index"
					});
				} else {
					reject(res);
				}
			})
			.catch((response) => {
				reject(response);
			});
	});
	return promise;
};
export default {
	BASE_URL,
	request,
};